﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Data;
using WPFExcelReport.Helpers;
using System.Linq;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public struct vvVKPOS
        {
            public string pos;
            public double vkpqty;
            public double allocated;
            public int rolls;
            public double difference;
        }
        public List<vvVKPOS> listVKPOS = new List<vvVKPOS>();
        public string lang = "CN";
        private void JLTVV03PK(DataSet db)
        {
            try
            {
                DataTable tbWB = db.Tables["WBBST"];
                DataTable tbVVT = db.Tables["VVTLIEF"];
                DataTable tbVKP = db.Tables["VKPOS"];
                Dictionary<string, string> makeup = new Dictionary<string, string>();
                Dictionary<string, string> packup = new Dictionary<string, string>();
                int rls = tbWB.Rows.Count;
                string processed = "";
                foreach (DataRow dwVVT in tbVVT.Rows)
                {
                    //VVTLIEFSCH VVTAUFKRS	VVTAUFLFD	VVTAUFPOS
                    DataRow dwVKPOS = null;
                    string VV = dwVVT["VVTLIEFSCH"].ToString();
                    string KRS = dwVVT["VVTAUFKRS"].ToString();
                    string LFD = dwVVT["VVTAUFLFD"].ToString();
                    string POS = dwVVT["VVTAUFPOS"].ToString();
                    string sheet = KRS + "-" + Helper.GetSubRollNum(LFD) + "-" + Helper.GetSubRollNum(POS);
                    string UM = "Y";
                    if (processed.Contains(KRS + LFD + POS + ";"))
                        continue;
                    processed += KRS + LFD + POS + ";";
                    var qvp = from DataRow dwTmp in tbVKP.AsEnumerable()
                              where dwTmp["VKPAUFKRS"].ToString() == KRS
                              && dwTmp["VKPAUFLFD"].ToString() == LFD
                              && dwTmp["VKPAUFPOS"].ToString() == POS
                              select dwTmp;
                    foreach (DataRow dwTmp in qvp)
                    {
                        dwVKPOS = dwTmp;
                    }
                    if (dwVKPOS == null)
                        continue;
                    if (dwVKPOS["VKPMEEH"].ToString().Contains("M"))
                        UM = "M";
                    string fileters = "WBBAUFKRS='" + KRS + "' and WBBAUFLFD='" + LFD + "' and WBBAUFPOS='" + POS + "' AND WBBALIEFSCH='" + VV + "'";
                    string sort = " WBBPACKNR ASC";
                    DataRow[] frolls = tbWB.Select(fileters,sort);
                    if (frolls.Length == 0)
                        continue;
                    exAgent.SetCurrentSheet("JLTVKWZ");
                    exAgent.CopySheet(sheet);
                    int maxlines = 44;
                    int x = 1;
                    int y = 1;
                    int pages = 0;
                    int turns = 0;
                    int rolls = 0;
                    int reserved = 12;
                    int TRolls = 0;
                    double vkptotal = 0;

                    exAgent.SetCurrentSheet(sheet);
                    exAgent.SetCellValue(1, 24, VV);
                    string str1 = "", str2 = "", str3 = "";
                    Helper.GetSplitDate(ref str1, ref str2, ref str3, date);
                    exAgent.SetCellValue(1, 30, str3);

                    string src = dwVKPOS["VKPKDARTNR"].ToString().TrimEnd() + "/" + dwVKPOS["VKPKDFARBNR"].ToString().TrimEnd() + "/" + dwVKPOS["VKPKDBEZ"].ToString().TrimEnd();
                    string cart = GetGB2312Value(src);

                    //exAgent.SetCellValue(3, 21, "客人品种:" + src);
                    string vkpart = dwVKPOS["VKPART"].ToString().TrimEnd() + "-" + dwVKPOS["VKPFARBE"].ToString();
                    //exAgent.SetCellValue(2, 21, "品       种:" + src); 
                    #region=====VAART===========
                    DataTable tbVA = db.Tables["VAART"];
                    DataRow dwVA07 = null;
                    string koll = dwVKPOS["VKPKOLL"].ToString();
                    string art = dwVKPOS["VKPART"].ToString();
                    DataRow[] rows = tbVA.Select("VAAKOLL='" + koll + "' AND VAAART='" + art + "'");
                    foreach (DataRow row in rows)
                    {
                        dwVA07 = row;
                    }
                    string articleinfor = "";
                    string sdkey = "";
                    DataTable tbVKK07 = new DataTable();
                    if (dwVA07 != null)
                    {
                        src = dwVA07["VAAARTGRP"].ToString();
                        sdkey = "00" + Left(src + "                                           ", 21) + "V";
                        src = INTEXDBC.GetSDDAT("0178", sdkey);
                        articleinfor += GetGB2312Value(src.Substring(1, 20)).Trim() + " ";

                        string text = "";
                        string TXTISIND = koll + art.Trim();
                        var qvad = from dwTemp in db.Tables["TXTKON"].AsEnumerable()
                                   where dwTemp["TXTIPIND"].ToString().Trim() == TXTISIND
                                   select dwTemp;
                        if (qvad.Count() > 0)
                        {
                            tbVKK07 = qvad.CopyToDataTable();
                        }
                        foreach (DataRow dwTXTKON in tbVKK07.Rows)
                        {
                            for (int ti = 1; ti < 40; ti++)
                            {
                                string ci = "TXT" + Right("00" + ti, 2) + "0";
                                src = dwTXTKON[ci].ToString(); src = GetGB2312Value(src).Trim();
                                if (src == "")
                                    break;
                                text += src;
                            }
                        }
                        articleinfor += text.Trim() + " ";

                        //exAgent.SetCellValue(6, 16, articleinfor);
                        string vbrt = dwVA07["VAAMEBRT"].ToString().Trim();
                        src = dwVA07["VAABRTBIS"].ToString();
                        double v2 = System.Math.Round(System.Convert.ToDouble(src) * 0.01, 0);
                        articleinfor += (v2 - 1) + "-" + v2 + vbrt.Trim();
                    }
                    exAgent.SetCellValue(5, 1, articleinfor);
                    exAgent.SetCellValue(6, 1, "*" + KRS + LFD + POS.Trim() + "*");
                    //exAgent.SetCellValue(6, 12, "*" + VV.Trim() + "*");
                    exAgent.SetCellValue(6, 12, "*" + VV.Trim() + "*");
                    #endregion
                    vvVKPOS vvvkp = FillInWBBST(exAgent, frolls.CopyToDataTable(), sheet, vkpart, cart, UM, lang);
                    listVKPOS.Add(vvvkp);
                    ExportWBList(tbWB, UM, lang);
                    ExportWBQUList(tbWB, UM, lang);
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                exAgent.DeleteSheet("JLTVKWZ");
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }

        }
        private void ExportWBQUList(DataTable dtSoruce, string UM, string m_lang)
        {
            exAgent.SetCurrentSheet("JLTWBQULIST");
            int x = 2, idx = 1;
            DataTable dtWBQU = new DataTable();
            dtWBQU.Columns.Add("WBBPACKNR", typeof(System.String)); exAgent.SetCellValue(x - 1, idx++, "卷号");
            dtWBQU.Columns.Add("WBBISTKLG", typeof(System.String)); exAgent.SetCellValue(x - 1, idx++, "匹长");
            dtWBQU.Columns.Add("WBBTAX", typeof(System.String)); exAgent.SetCellValue(x - 1, idx++, "分色");
            dtWBQU.Columns.Add("WBBDPART", typeof(System.String)); exAgent.SetCellValue(x - 1, idx++, "格型");
            dtWBQU.Columns.Add("WBBQU", typeof(System.String)); exAgent.SetCellValue(x - 1, idx++, "等级");
            dtWBQU.Columns.Add("REMARK", typeof(System.String)); exAgent.SetCellValue(x - 1, idx++, "备注");
            
            string filter = "REMARK<>'XX' ";
            string sorts = "WBBPACKNR ASC";
            DataRow[] rows = dtSoruce.Select(filter, sorts);
            DataTable tb = rows.CopyToDataTable();
            dtWBQU.Merge(tb, false, MissingSchemaAction.Ignore);
            foreach (DataRow row in dtWBQU.Rows)
            {
                double RAWLG = 0.001 * Convert.ToDouble(row["WBBISTKLG"].ToString());
                double STKLG = System.Math.Round(RAWLG, 1);
                double YLength = System.Math.Round(RAWLG / 0.9144, 1);
                if (UM == "Y")
                    STKLG = YLength;
                row["WBBISTKLG"] = STKLG;
                row["REMARK"] = GetGB2312Value(row["REMARK"].ToString());

            }
            int count = dtWBQU.Rows.Count;
            exAgent.DataTableToExcel(dtWBQU, x, 1);
            exAgent.SetCellValue(1, 1, 1);
            exAgent.BatchConvert2Number(x, 1, count + x-1, 2);
            exAgent.SetCellValue(1, 1, "卷号");
            exAgent.RenameSheet("质量信息");

        }
        private void ExportWBList(DataTable dtSoruce,string UM,string m_lang)
        {
            exAgent.SetCurrentSheet("JLTWBLIST");
            int idx = 2; 
            DataTable dtOutput = new DataTable();
            int x = 4;
            dtOutput.Columns.Add("WBBPACKNR", typeof(System.String)); exAgent.SetCellValue(x-1, idx++, "卷号");
            dtOutput.Columns.Add("WBBISTKLG", typeof(System.String)); exAgent.SetCellValue(x-1, idx++, "匹长");
            dtOutput.Columns.Add("WBBTAX", typeof(System.String)); exAgent.SetCellValue(x-1, idx++, "分色");
            dtOutput.Columns.Add("WBBDPART", typeof(System.String)); exAgent.SetCellValue(x-1, idx++, "格型");

            string filter = "WBBPACKNR<>'x'";
            string sorts = "WBBPACKNR ASC";
            DataRow[] rows = dtSoruce.Select(filter, sorts);
            DataTable tb = rows.CopyToDataTable();
            dtOutput.Merge(tb, false, MissingSchemaAction.Ignore);
            foreach (DataRow row in dtOutput.Rows)
            {
                double RAWLG =0.001* Convert.ToDouble(row["WBBISTKLG"].ToString()); 
                double STKLG = System.Math.Round(RAWLG, 1);
                double YLength = System.Math.Round(RAWLG / 0.9144, 1);
                if (UM == "Y")
                    STKLG = YLength;
                row["WBBISTKLG"] = STKLG;
                
            }
            int count = dtOutput.Rows.Count;
            //exAgent.RangeCopy(x, 10, x + count, 10,true);
            exAgent.DataTableToExcel(dtOutput, x, 2);           
            exAgent.SetCellValue(1, 1, 1);
            exAgent.BatchConvert2Number(x, 2, count + x, 3); 
            exAgent.SetCellValue(1, 1, date);
            exAgent.RenameSheet("布卷清单");

           
        }
        public vvVKPOS FillInWBBST(ExcelX.ExcelLib.ExcelAgent m_exAgent, DataTable tbWB, string sheet, string vkpart, string cart, string UM, string m_Lang)
        {
            double vkptotal = 0;
            string processed = "";
            
            vvVKPOS vvvkp = new vvVKPOS();
            try
            {
                int maxlines = 39;
                int x = 1;
                int y = 1;
                int pages = 0;
                int turns = 0;
                int rolls = 0;
                int reserved = 12;
                int TRolls = 0;
                if (m_Lang == "EN")
                {
                    m_exAgent.SetCellValue(3, 8, "Allocation");
                    m_exAgent.SetCellValue(1, 23, "Order:" + sheet);
                    m_exAgent.SetCellValue(2, 23, "Article:" + vkpart);
                    m_exAgent.SetCellValue(1, 30, "Print:" + date);
                    m_exAgent.SetCellValue(3, 20, "Customer Art:" + cart);
                    m_exAgent.SetCellValue(pages * 61 + 11, 1, "C/NO.");
                    m_exAgent.SetCellValue(pages * 61 + 11, 3, "LENGTH");
                    m_exAgent.SetCellValue(pages * 61 + 11, 6, "LOT");
                    m_exAgent.SetCellValue(pages * 61 + 11, 8, "PATTERN");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 1, "C/NO.");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 3, "LENGTH");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 6, "LOT");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 8, "PATTERN");

                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 1, "C/NO.");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 3, "LENGTH");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 6, "LOT");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 8, "PATTERN");

                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 1, "C/NO.");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 3, "LENGTH");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 6, "LOT");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 8, "PATTERN");
                }
                else
                {
                    m_exAgent.SetCellValue(3, 8, "留位清单");
                    m_exAgent.SetCellValue(1, 23, "销售单号:" + sheet);
                    m_exAgent.SetCellValue(2, 23, "品       种:" + vkpart);
                    m_exAgent.SetCellValue(1, 30, "打印:" + date);
                    m_exAgent.SetCellValue(3, 20, "客人品种:" + cart);
                    m_exAgent.SetCellValue(pages * 61 + 11, 1, "卷号");
                    m_exAgent.SetCellValue(pages * 61 + 11, 3, "匹长");
                    m_exAgent.SetCellValue(pages * 61 + 11, 6, "分色");
                    m_exAgent.SetCellValue(pages * 61 + 11, 8, "格型");

                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 1, "卷号");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 3, "匹长");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 6, "分色");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 8, "格型");

                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 1, "卷号");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 3, "匹长");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 6, "分色");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 8, "格型");

                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 1, "卷号");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 3, "匹长");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 6, "分色");
                    m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 8, "格型");
                }

                double lineqty = 0;
                int lineroll = 0;

                foreach (DataRow rwb in tbWB.Rows)
                {
                    //pages =0,rolls=0,turns=0
                    //pages=0,rolls=37
                    if (rolls > maxlines)
                    {
                        rolls = 0;
                        lineqty = 0;
                        lineroll = 0;
                        turns++;
                        if (turns > 3)
                        {
                            turns = 0;
                            pages++;
                            #region=====Header===

                            m_exAgent.RangeCopy(11, 51, pages * 61 + 11, (pages) * 61+51, true);
                            exAgent.ShowRows(11, 1,(pages+1) * 61,1);
                            if (m_Lang == "EN")
                            {
                                m_exAgent.SetCellValue(pages * 61 + 11, 1, "C/NO.");
                                m_exAgent.SetCellValue(pages * 61 + 11, 3, "LENGTH");
                                m_exAgent.SetCellValue(pages * 61 + 11, 6, "LOT");
                                m_exAgent.SetCellValue(pages * 61 + 11, 8, "PATTERN");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 1, "C/NO.");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 3, "LENGTH");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 6, "LOT");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 8, "PATTERN");

                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 1, "C/NO.");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 3, "LENGTH");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 6, "LOT");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 8, "PATTERN");

                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 1, "C/NO.");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 3, "LENGTH");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 6, "LOT");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 8, "PATTERN");
                            }
                            else
                            {
                                m_exAgent.SetCellValue(pages * 61 + 11, 1, "卷号");
                                m_exAgent.SetCellValue(pages * 61 + 11, 3, "匹长");
                                m_exAgent.SetCellValue(pages * 61 + 11, 6, "分色");
                                m_exAgent.SetCellValue(pages * 61 + 11, 8, "格型");

                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 1, "卷号");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 3, "匹长");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 6, "分色");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 1 + 8, "格型");

                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 1, "卷号");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 3, "匹长");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 6, "分色");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 2 + 8, "格型");

                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 1, "卷号");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 3, "匹长");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 6, "分色");
                                m_exAgent.SetCellValue(pages * 61 + 11, 9 * 3 + 8, "格型");
                            }
                            #endregion
                        }
                    }
                    if (pages > 0)
                    {
                        reserved = 12;
                        maxlines = 39;
                    }
                    x = pages * 61 + reserved + rolls;
                    y = turns * 9 + 1;

                    string stk = rwb["WBBSTKNR"].ToString();
                    double stkweight = 0;
                    double stkgrossw = 0;
                    //INTEXDBC.GetStkWeight(rwb, ref  stkgrossw, ref  stkweight, ref makeup, ref  packup);

                    double RAWLG = System.Convert.ToDouble(rwb["WBBISTKLG"].ToString()) * 0.001;
                    double STKLG = System.Math.Round(RAWLG, 1);
                    double YLength = System.Math.Round(RAWLG / 0.9144, 1);
                    string cardno = rwb["WBBRWLFD"].ToString().Trim();
                    string packnr = rwb["WBBPACKNR"].ToString().Trim();

                    if (packnr != "0000")
                    {
                        if (processed.Contains(packnr + ";")) continue;
                        processed += packnr + ";";
                        stk = Helper.GetSubRollNum(packnr);
                        var qxd = from XD in tbWB.AsEnumerable()
                                  where XD["WBBPACKNR"].ToString() == packnr
                                  group XD by new
                                  {
                                      WBBPACKNR = XD["WBBPACKNR"].ToString(),
                                  }
                                      into g
                                      select new
                                      {
                                          KEY = g.Key,
                                          RAWLG = g.Sum(XD => XD.Field<decimal>("WBBISTKLG")),
                                      };
                        if (qxd.Count() > 0)
                        {
                            foreach (var xd in qxd)
                            {
                                RAWLG = 0.001 * (double)xd.RAWLG;
                                STKLG = System.Math.Round(RAWLG, 1);
                            }
                        }
                    }
                    else
                    {
                        stk = Helper.GetAbsRollNum(stk, cardno);
                    }

                    if (UM == "Y")
                        STKLG = YLength;
                    m_exAgent.SetCellValue(x, y, stk);
                    m_exAgent.SetCellValue(x, y + 2, STKLG);
                    m_exAgent.SetCellValue(x, y + 5, rwb["WBBTAX"].ToString().Trim());
                    m_exAgent.SetCellValue(x, y + 7, rwb["WBBDPART"].ToString().Trim());
                    //m_exAgent.SetCellValue(x, y + 8, stkgrossw);
                    rolls++;
                    TRolls++;
                    lineqty += STKLG;
                    lineroll++;
                    x = pages * 61 + 52;
                    y = turns * 9 + 1;
                    //m_exAgent.SetCellValue(x, y + 2, lineqty + UM);
                    //m_exAgent.SetCellValue(x, y + 5, lineroll + "PCS");
                   
                    vkptotal += STKLG;
                }
                if (m_Lang == "EN")
                {
                    //m_exAgent.SetCellValue(5, 21, "Total:" + System.Math.Round(vkptotal, 1) + UM + ";" + TRolls + "PCS");
                    m_exAgent.SetCellValue(5, 29, UM);
                }
                else
                {
                    //m_exAgent.SetCellValue(5, 21, "总长度:" + System.Math.Round(vkptotal, 1) + UM + ";总卷数" + TRolls + "个");
                    m_exAgent.SetCellValue(5, 29,  UM );
                }

                vvvkp.pos = sheet;
                vvvkp.allocated = System.Math.Round(vkptotal, 1);
                vvvkp.rolls = TRolls;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }
            return vvvkp;
        }
    
    }
}
